package com.hzqy.web.ac.epg;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.service.epg.BianfengService;
import com.hzqy.service.epg.WchatTvOrgService;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.WchatTvUserVo;
import com.hzqy.web.vo.epg.WchatTvOrgVo;

@RequestMapping(value="/")
@Controller
public class WchatTvOrgAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private WchatTvOrgService service;
	
	@Resource
	private BianfengService bianfengService;

	/**
	 * 查询本地绑定情况
	 * @param activityId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/watchtv/selectByUserId")
	@ResponseBody
	public Object selectByUserId(String activityId, HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		String userID = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		/*String tokenkey = epgCommonAction.token(userID);
		String attribute = String.valueOf( session.getAttribute("EPGverify"+userID));
		if (attribute=="null" || !attribute.equals(tokenkey)) {
			LOG.debug("恶意攻击接口,session未获取到EPGverify:" + attribute);
			return map;
		}*/
		WchatTvUserVo wchatTvUserVo = new WchatTvUserVo();
		wchatTvUserVo.setF_wtut_serverid(userID);
		map = service.selectByUserId(wchatTvUserVo);
		return map;
	}
	
	/***
	 * 本地账号手机号绑定
	 * @param activityId
	 * @param phone  
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/watchtv/UpdateUserId")
	@ResponseBody
	public Object UpdateUserId(String activityId,String phone, HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		String userID = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		/*String tokenkey = epgCommonAction.token(userID);
		String attribute = String.valueOf( session.getAttribute("EPGverify"+userID));
		if (attribute=="null" || !attribute.equals(tokenkey)) {
			LOG.debug("恶意攻击接口,session未获取到EPGverify:" + attribute);
			return map;
		}*/
		WchatTvUserVo wchatTvUserVo = new WchatTvUserVo();
		wchatTvUserVo.setF_wtut_phone(phone);
		wchatTvUserVo.setF_wtut_serverid(userID);
		map = service.selectByUserPhone(wchatTvUserVo);
		return map;
	}
	

	@RequestMapping(value = "/watchtv/selectWchatTvUserInfo")
	@ResponseBody
	public Object selectWchatTvUserInfo(String f_wtut_openid,String f_wtut_serverid) {
		Map<String,Object> map = new HashMap<String,Object>();
		WchatTvUserVo wchatTvUserVo = new WchatTvUserVo();
		wchatTvUserVo = service.selectWchatTvUserInfo(f_wtut_openid,f_wtut_serverid);
		LOG.debug(wchatTvUserVo+"");
		if(wchatTvUserVo == null) {
			map.put("msg", "false");
			return map;
		}else {
			return  wchatTvUserVo;
		}
	}

	@RequestMapping(value = "/watchtv/selectPhone")
	@ResponseBody
	public Object selectWchatPhone(String userid, HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		HttpSession session = request.getSession();
		String userID = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		WchatTvUserVo tvUserVo = service.getPhone(userID);
		if(tvUserVo != null){
			LOG.debug("查询手机号userID："+userID+"手机号信息："+tvUserVo.getF_wtut_phone()+"报名成功1状态成功"+tvUserVo.getF_wtut_status());
			if (tvUserVo.getF_wtut_phone() == null || "".equals(tvUserVo.getF_wtut_phone())) {
			map.put("phone", "0");
			} else {
			map.put("phone", tvUserVo.getF_wtut_phone());
			}
			map.put("status",String.valueOf(tvUserVo.getF_wtut_status()));
		}else{
		map.put("phone",0);
		map.put("status","3");
		}
		return map;
	}

}
